# Ubuntu下升级gcc跟g++

- Link: https://yjyj.net/learn/4609.html
- Published: 2014-04-19T19:29:52+08:00
- Author: 小 虾

⚡ 生猛大蝦编辑部 · 30 秒核心看点

 * 先添加工具链源，再安装 gcc-4.8 和 g++-4.8，避免系统默认版本过旧。
 * 用 update-alternatives 重新绑定 gcc/g++ 默认指向，切换后再确认版本。
 * 若提示 add-apt-repository 找不到，先装 python-software-properties，再继续升级。

好吧,TrinityCore需要4.72版本的gcc做支撑.哥的是4.6.
 只好再给它生个级了.次奥啊 装
个TrinityCore,基本上要把哥服务器都给升级个遍了马蛋

干脆给他装个4.8版本的!

过程如下:

    ```decode:true
    sudo add-apt-repository ppa:ubuntu-toolchain-r/test
    sudo update-alternatives --remove-all gcc 
    sudo update-alternatives --remove-all g++
    sudo apt-get install gcc-4.8
    sudo apt-get install g++-4.8
    sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 20
    sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.8 20
    sudo update-alternatives --config gcc
    sudo update-alternatives --config g++

    sudo apt-get update
    sudo apt-get upgrade -y
    sudo apt-get dist-upgrade
    ```

如果安装时出现:

    ```decode:true
    add-apt-repository: command not found
    ```

解决方法：安装工具包

    ```decode:true
    apt-get install python-software-properties
    ```

安装成功后, 即可通过 gcc –version 命令查看当前版本.

❓ 延伸问答与排错指南（FAQ）

Ubuntu 下怎么把 gcc 升到指定版本？

先添加对应的软件源，再安装目标版本的 gcc 和 g++，随后用 update-alternatives 把 /
usr/bin/gcc 和 /usr/bin/g++ 指向新版本，最后用 gcc --version 检查是否切换成功。

update-alternatives 切换 gcc 后没生效怎么办？

先确认已安装目标版本的 gcc/g++，再分别执行 --install 和 --config 重新选择默认项。
如果仍未生效，检查是否有旧的 alternatives 记录残留，必要时先 remove-all 再重新绑
定。

add-apt-repository 提示找不到命令怎么处理？

这是工具包缺失导致的，先安装 python-software-properties，再重新执行添加源命令。装
好后再继续后续的 apt-get update、安装和版本切换步骤。

📚 【[招数学习](https://yjyj.net/category/learn)】专栏延伸阅读与热门精选（[全站文章归档](https://yjyj.net/archive)）：

 * [Ubuntu下安装最新版本的 CMake](https://yjyj.net/learn/centos/4606.html)
 * [密码保护：服务器又粗了问题,这次索性装个传说中的TrintyCore试试看~](https://yjyj.net/learn/4602.html)
 * [tomcat配置文件server.xml详解](https://yjyj.net/learn/4618.html)
 * [解决Windows 7 任务栏windows资源管理器“最近”和“已固定”消失的问题](https://yjyj.net/learn/4621.html)
 * 热门必读[升级一下iptables](https://yjyj.net/learn/centos/3208.html)
 * 热门必读[京东云路由器雅典娜 AX6600 折腾记](https://yjyj.net/learn/7810.html)

## Comments

### By: 小虾

- Link: https://yjyj.net/learn/4609.html/comment-page-1#comment-12960
- Published: 2014-10-16T11:03:57+08:00

如果木有 请执行 sudo apt-get update
