remrin

remrin

github
email

Setting up a standalone Linux development environment in MacOS

Preface#

Using a Linux development environment in Mac OS itself is not difficult because Mac OS itself is a Unix-like system, and with HomeBrew, you can directly install most of the Linux software packages. Even without using a virtual machine, you can still have a relatively good development experience. However, there may be special requirements or you may not want to pollute the host machine's development environment. In that case, you can use virtualization software such as VMware Fusion, Parallels Desktop, or VirtualBox to install a complete Linux distribution in a virtual machine. However, these types of virtual machines are relatively heavy, slow to start, and consume a large amount of memory, so they are not within my consideration. Of course, you can also use Docker Desktop to directly use various Linux distribution images or create a Dev Container to create a fully equipped development image. However, there is currently no permanent solution to the problem of Docker image sources, so it is also not within my consideration. In the end, I decided to use Machines in Orbstack to build a development environment.

Installing Orbstack#

You can directly download the installation package from the official website, or install it directly using HomeBrew.

brew install orbstack

Choosing a suitable distribution#

I am using Fedora here.
orbstack software
Fedora

Setting up the development environment#

  1. The first step is to switch to the Tsinghua mirror and back up the default repository.
sudo cp -r /etc/yum.repos.d /etc/yum.repos.d.backup

Then replace the default repository.

sudo sed -e 's|^metalink=|#metalink=|g' \
    -e 's|^#baseurl=http://download.example/pub/fedora/linux|baseurl=https://mirrors.tuna.tsinghua.edu.cn/fedora|g' \
    -i.bak \
    /etc/yum.repos.d/fedora.repo \
    /etc/yum.repos.d/fedora-modular.repo \
    /etc/yum.repos.d/fedora-updates.repo \
    /etc/yum.repos.d/fedora-updates-modular.repo

If you prefer to switch manually:

  • Fedora repository (/etc/yum.repos.d/fedora.repo)
[fedora]
name=Fedora $releasever - $basearch
failovermethod=priority
baseurl=https://mirrors.tuna.tsinghua.edu.cn/fedora/releases/$releasever/Everything/$basearch/os/
metadata_expire=28d
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-$releasever-$basearch
skip_if_unavailable=False
  • Updates repository (/etc/yum.repos.d/fedora-updates.repo)
[updates]
name=Fedora $releasever - $basearch - Updates
failovermethod=priority
baseurl=https://mirrors.tuna.tsinghua.edu.cn/fedora/updates/$releasever/Everything/$basearch/
enabled=1
gpgcheck=1
metadata_expire=6h
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-$releasever-$basearch
skip_if_unavailable=False
  • Fedora-modular repository (/etc/yum.repos.d/fedora-modular.repo)
[fedora-modular]
name=Fedora Modular $releasever - $basearch
failovermethod=priority
baseurl=https://mirrors.tuna.tsinghua.edu.cn/fedora/releases/$releasever/Modular/$basearch/os/
enabled=1
metadata_expire=7d
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-$releasever-$basearch
skip_if_unavailable=False
  • Updates-modular repository (/etc/yum.repos.d/fedora-updates-modular.repo)
[updates-modular]
name=Fedora Modular $releasever - $basearch - Updates
failovermethod=priority
baseurl=https://mirrors.tuna.tsinghua.edu.cn/fedora/updates/$releasever/Modular/$basearch/
enabled=1
gpgcheck=1
metadata_expire=6h
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-$releasever-$basearch
skip_if_unavailable=False
  • Clean the cache
sudo dnf clean all
sudo dnf makecache
  • Update the software packages
sudo dnf update

  1. Replace the default bash with zsh
  • Install git and zsh
sudo dnf install git zsh
  • Install oh my zsh
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
  • Install commonly used oh my zsh plugins
    • Autosuggestions
git clone https://github.com/zsh-users/zsh-autosuggestions.git $ZSH_CUSTOM/plugins/zsh-autosuggestions
  • Syntax highlighting
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git $ZSH_CUSTOM/plugins/zsh-syntax-highlighting
git clone https://github.com/zdharma-continuum/fast-syntax-highlighting.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/plugins/fast-syntax-highlighting
  • Autocomplete
git clone --depth 1 -- https://github.com/marlonrichert/zsh-autocomplete.git $ZSH_CUSTOM/plugins/zsh-autocomplete
  • Modify the zsh configuration file and change the plugins section to
vim ~/.zshrc
plugins=(
  git
  zsh-autosuggestions
  zsh-syntax-highlighting
  fast-syntax-highlighting
  zsh-autocomplete
)
  • Apply the changes
source ~/.zshrc
  1. Install NVM to manage NodeJS versions
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
  • How to use
$ nvm use 16
Now using node v16.9.1 (npm v7.21.1)
$ node -v
v16.9.1
$ nvm use 14
Now using node v14.18.0 (npm v6.14.15)
$ node -v
v14.18.0
$ nvm install 12
Now using node v12.22.6 (npm v6.14.5)
$ node -v
v12.22.6

For more advanced usage, please refer to the documentation.

  1. Connect to the virtual machine using VS Code
  • Install the Remote Development extension
  • Add a remote connection by selecting Remote Explorer in the left menu, clicking the + button on SSH, or using the shortcut Shift+Command+P and entering remote add to open the window for adding an SSH connection.
    image
    Enter ssh orb to connect to the virtual machine you just created.
    ::: warning
    If you have multiple virtual machines, you need to specify which host to connect to when using SSH. If not specified, it will connect to the default host. For example, ssh debain@orb connects to the debain host using the default user. Examples: ssh machine@orb, ssh user@orb, ssh user@machine@orb.
    :::
    image
    image

This way, you have an isolated development environment that is not affected by the host system. It is somewhat similar to WSL in Windows, but it is simpler and more convenient, without the need to enable certain system features. After downloading Orbstack, you can use it directly.

This article is synchronized and updated to xLog by Mix Space.
The original link is https://remrin.dev/posts/linux/dev-env


Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.