メインコンテンツまでスキップ

Ubuntu Setup Guide

This comprehensive guide covers setting up a complete Ubuntu development environment, including package managers, development tools, GPU support, and machine learning frameworks.

0. Pre-installation: Remove Existing Ubuntu (If Needed)

0.1 Remove Ubuntu from Dual Boot System

If you need to remove an existing Ubuntu installation from a dual boot system:

Open Diskpart (Windows)

  1. Press Win + R and type diskpart
  2. Press Enter to open Diskpart with administrator privileges

Commands in Diskpart

Check available disks:

list disk

Select the disk where Ubuntu is installed:

select disk <disk_number>

Check partitions on the selected disk:

list partition

Select the partition containing Ubuntu system files:

select partition 1

Assign a drive letter for access:

assign letter=P

Delete Ubuntu System Files

  1. Run Notepad as Administrator
  2. Navigate to P: in File Explorer
  3. Delete the Ubuntu folder in the EFI directory

Remove Assigned Drive Letter

Return to Diskpart and remove the temporary drive letter:

remove letter=P

Delete Ubuntu Partition Using Disk Management

  1. Open Disk Management (Win + XDisk Management)
  2. Locate the partition containing Ubuntu
  3. Right-click and select "Delete Volume"
  4. Optional: Extend another partition into the unallocated space
Important

Always backup important data before modifying disk partitions. Incorrect operations can result in data loss.

1. Install Ubuntu

1.1 Download Ubuntu

1.2 Create Installation Media

Use Rufus to create a bootable USB drive:

  1. Download and run Rufus
  2. Select your USB drive
  3. Select the Ubuntu ISO file
  4. Use default settings and click "START"

1.3 Installation Tips

  • Choose "Install Ubuntu alongside Windows" for dual boot
  • Allocate at least 50GB for Ubuntu partition
  • Create a separate home partition if desired

2. Initial System Setup

2.1 Update System Packages

# Update package lists
sudo apt update

# Upgrade all packages
sudo apt upgrade -y

# Install essential packages
sudo apt install -y curl wget git vim build-essential software-properties-common apt-transport-https ca-certificates gnupg lsb-release

2.2 Install Additional Codecs and Media Support

# Install multimedia codecs
sudo apt install -y ubuntu-restricted-extras

# Install additional media codecs
sudo apt install -y ffmpeg

3. Install Anaconda3

3.1 Download and Install

# Navigate to Downloads directory
cd ~/Downloads

# Download Anaconda (replace with latest version)
wget https://repo.anaconda.com/archive/Anaconda3-2023.09-0-Linux-x86_64.sh

# Make the installer executable
chmod +x Anaconda3-*.sh

# Run the installer
bash Anaconda3-*.sh

Follow the installation prompts:

  • Press Enter to review the license
  • Type "yes" to accept the license terms
  • Press Enter to confirm the installation location
  • Type "yes" when asked to initialize Anaconda3

3.2 Configure Environment

# Edit bashrc to add Anaconda to PATH
nano ~/.bashrc

Add the following line (replace username with your actual username):

export PATH="/home/username/anaconda3/bin:$PATH"

Reload the bashrc:

source ~/.bashrc

3.3 Essential Conda Commands

# Create a new environment
conda create -n your_env_name python=3.9

# Activate environment
conda activate your_env_name

# Deactivate environment
conda deactivate

# Remove environment
conda remove -n your_env_name --all

# List all environments
conda env list
# or
conda info --envs

# List installed packages
conda list

# Install packages
conda install package_name
conda install scrapy==1.3
conda install -n env_name package_name

# Update conda
conda update conda

# Update all packages
conda update --all

# Update Anaconda
conda update anaconda

# Update Python
conda update python

3.4 Set Up Conda Channels

# Add conda-forge channel (recommended)
conda config --add channels conda-forge

# Set conda-forge as priority
conda config --set channel_priority strict

4. Install PyCharm

4.1 Download PyCharm

Download PyCharm (Community or Professional):

# Navigate to Downloads
cd ~/Downloads

# Extract PyCharm
tar -xzf pycharm-*.tar.gz

# Move to /opt directory
sudo mv pycharm-* /opt/pycharm

# Create desktop shortcut
sudo ln -s /opt/pycharm/bin/pycharm.sh /usr/local/bin/pycharm

4.2 Alternative: Install via Snap

# Install PyCharm Community Edition
sudo snap install pycharm-community --classic

# Install PyCharm Professional Edition
sudo snap install pycharm-professional --classic

4.3 Configure PyCharm with Conda

  1. Open PyCharm
  2. Go to File → Settings → Project → Python Interpreter
  3. Click gear icon → Add → Conda Environment
  4. Select existing environment or create new one

5. Install ROS (Robot Operating System)

ROS2 Humble (Ubuntu 22.04)

# Add ROS2 repository
sudo apt update && sudo apt install curl gnupg lsb-release
sudo curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.key -o /usr/share/keyrings/ros-archive-keyring.gpg

echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/ros-archive-keyring.gpg] http://packages.ros.org/ros2/ubuntu $(source /etc/os-release && echo $UBUNTU_CODENAME) main" | sudo tee /etc/apt/sources.list.d/ros2.list > /dev/null

# Install ROS2 Humble
sudo apt update
sudo apt install -y ros-humble-desktop-full

# Install development tools
sudo apt install -y python3-colcon-common-extensions python3-rosdep

# Initialize rosdep
sudo rosdep init
rosdep update

# Source ROS2 setup
echo "source /opt/ros/humble/setup.bash" >> ~/.bashrc
source ~/.bashrc

ROS1 Noetic (Ubuntu 20.04)

# Add ROS repository
sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'

# Add ROS keys
sudo apt-key adv --keyserver 'hkp://keyserver.ubuntu.com:80' --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654

# Install ROS Noetic
sudo apt update
sudo apt install -y ros-noetic-desktop-full

# Install rosdep
sudo apt install -y python3-rosdep python3-rosinstall python3-rosinstall-generator python3-wstool build-essential

# Initialize rosdep
sudo rosdep init
rosdep update

# Source ROS setup
echo "source /opt/ros/noetic/setup.bash" >> ~/.bashrc
source ~/.bashrc

6. Install NVIDIA Driver

6.1 Check Available Drivers

# Check available NVIDIA drivers
ubuntu-drivers devices
# Install the recommended driver (replace with your version)
sudo apt install nvidia-driver-535

# Alternative: Install automatically recommended driver
sudo ubuntu-drivers autoinstall

6.3 Handle MOK (Machine Owner Key)

If you see "Enroll MOK" during boot:

  1. Select "Enroll MOK"
  2. Select "Continue"
  3. Enter the password you set during driver installation
  4. Reboot

6.4 Verify Installation

# Check NVIDIA driver installation
nvidia-smi

Expected output should show your GPU information and driver version.

7. Install CUDA Toolkit

7.1 Download and Install CUDA

Visit NVIDIA CUDA Toolkit Archive and download the appropriate version.

# Example for CUDA 12.3 (adjust URL for your version)
wget https://developer.download.nvidia.com/compute/cuda/12.3.0/local_installers/cuda_12.3.0_545.23.06_linux.run

# Make executable
chmod +x cuda_12.3.0_*.run

# Install CUDA
sudo sh cuda_12.3.0_*.run

During installation:

  • Deselect "Driver" (since we already installed it)
  • Select "CUDA Toolkit"

7.2 Configure Environment Variables

# Edit bashrc
nano ~/.bashrc

Add these lines (adjust version number as needed):

export PATH=/usr/local/cuda-12.3/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-12.3/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}

Reload configuration:

source ~/.bashrc

7.3 Verify CUDA Installation

# Check CUDA version
nvcc --version

# Check CUDA samples (if installed)
cd /usr/local/cuda/samples/1_Utilities/deviceQuery
sudo make
./deviceQuery

8. Install cuDNN

8.1 Download cuDNN

  1. Visit cuDNN Archive
  2. Create NVIDIA Developer account if needed
  3. Download cuDNN for your CUDA version

8.2 Install cuDNN

# Navigate to Downloads
cd ~/Downloads

# Extract cuDNN
tar -xzf cudnn-*.tar.xz

# Navigate to extracted directory
cd cudnn-*

# Copy files to CUDA installation
sudo cp include/cudnn*.h /usr/local/cuda/include
sudo cp -P lib/libcudnn* /usr/local/cuda/lib64
sudo chmod a+r /usr/local/cuda/include/cudnn*.h /usr/local/cuda/lib64/libcudnn*

8.3 Verify cuDNN Installation

# Check cuDNN version
cat /usr/local/cuda/include/cudnn_version.h | grep CUDNN_MAJOR -A 2

9. Install PyTorch

9.1 Install PyTorch with CUDA Support

Visit PyTorch Previous Versions for specific versions.

# Activate your conda environment
conda activate your_env_name

# Install PyTorch with CUDA support (example for CUDA 12.1)
conda install pytorch torchvision torchaudio pytorch-cuda=12.1 -c pytorch -c nvidia

# Alternative: pip installation
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121

9.2 Verify PyTorch Installation

Create a test script:

import torch

print(f"PyTorch version: {torch.__version__}")
print(f"CUDA version: {torch.version.cuda}")
print(f"cuDNN version: {torch.backends.cudnn.version()}")
print(f"CUDA available: {torch.cuda.is_available()}")
print(f"CUDA device count: {torch.cuda.device_count()}")

if torch.cuda.is_available():
print(f"CUDA device name: {torch.cuda.get_device_name(0)}")
print(f"Current CUDA device: {torch.cuda.current_device()}")

Run the script:

python test_pytorch.py

10. Install Docker

10.1 Install Docker Engine

# Remove old versions
sudo apt-get remove docker docker-engine docker.io containerd runc

# Add Docker's official GPG key
sudo apt-get update
sudo apt-get install ca-certificates curl gnupg
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg

# Add repository
echo \
"deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
"$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

# Install Docker Engine
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

10.2 Configure Docker for Non-root User

# Add user to docker group
sudo groupadd docker
sudo usermod -aG docker $USER

# Apply changes
newgrp docker

# Test installation
docker run hello-world
Docker Desktop

Do not install Docker Desktop on Ubuntu. Docker Engine is sufficient and more lightweight.

11. Additional Development Tools

11.1 Install VS Code

# Download and install VS Code
wget -qO- https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > packages.microsoft.gpg
sudo install -o root -g root -m 644 packages.microsoft.gpg /etc/apt/trusted.gpg.d/
echo "deb [arch=amd64,arm64,armhf signed-by=/etc/apt/trusted.gpg.d/packages.microsoft.gpg] https://packages.microsoft.com/repos/code stable main" | sudo tee /etc/apt/sources.list.d/vscode.list

sudo apt update
sudo apt install code

11.2 Install Node.js and npm

# Install Node.js LTS
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt-get install -y nodejs

# Verify installation
node --version
npm --version

11.3 Install Additional Programming Languages

Java Development Kit

# Install OpenJDK
sudo apt install default-jdk

# Verify installation
java --version
javac --version

Go Programming Language

# Install Go
sudo apt install golang-go

# Verify installation
go version

12. System Optimization

12.1 Install System Monitoring Tools

# Install htop, neofetch, and other useful tools
sudo apt install htop neofetch tree tmux screen

12.2 Enable Firewall

# Enable UFW firewall
sudo ufw enable

# Check status
sudo ufw status

12.3 Set Up Automatic Updates

# Install unattended-upgrades
sudo apt install unattended-upgrades

# Configure automatic updates
sudo dpkg-reconfigure unattended-upgrades

13. Backup and Recovery

13.1 Create System Backup

# Install timeshift for system snapshots
sudo apt install timeshift

# Create backup via GUI
sudo timeshift-gtk

13.2 Backup Configuration Files

# Backup important configuration files
mkdir ~/config-backup
cp ~/.bashrc ~/config-backup/
cp ~/.profile ~/config-backup/
# Add other important configs as needed

Troubleshooting

Common Issues and Solutions

NVIDIA Driver Issues

# Purge and reinstall NVIDIA drivers
sudo apt purge nvidia-*
sudo ubuntu-drivers autoinstall
sudo reboot

CUDA Path Issues

# Check CUDA installation path
ls /usr/local/cuda*

# Update paths in ~/.bashrc accordingly

Python Environment Conflicts

# Reset conda environment
conda remove --name myenv --all
conda create --name myenv python=3.9
System Maintenance
  • Regularly update your system with sudo apt update && sudo apt upgrade
  • Keep your conda environments clean and organized
  • Use virtual environments for different projects
  • Monitor system resources with htop and nvidia-smi

Resources